package com.apes.autoparts.product.repository;

import com.apes.framework.query.ApesDao;
import com.apes.framework.util.MapUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 类描述：
 *
 * @ClassName OriginalProductDao
 * @Description 描述
 * @Author wushilong
 * @Date 2023/3/16 11:23
 * @Version 1.0
 */
@Component("originalProductDao")
public class OriginalProductDao extends ApesDao {

    public List<Map<String, String>> getEPC(String automobileBrand){
        String sql = "SELECT DISTINCT EPC_BBMC FROM EPC_BBMC WHERE QCPPMC = :automobileBrand";
        return find(sql, MapUtil.mapper("automobileBrand", automobileBrand));
    }

    public JSONArray findAll(JSONObject param){
        String sql = "SELECT * FROM EPC_BBMC WHERE 1=1 ";
        if (param.getString("autoBrand") != null && !"".equals(param.getString("autoBrand")))
            sql += " AND A.QCPPMC = '" + param.getString("autoBrand") + "'";
        if (param.getString("autoLabel") != null && !"".equals(param.getString("autoLabel")))
            sql += " AND A.QCCPMC = '" + param.getString("autoLabel") + "'";
        if (param.getString("epcVersion") != null && !"".equals(param.getString("epcVersion")))
            sql += " AND A.EPC_BBMC = '" + param.getString("epcVersion") + "'";
        return toJSONArray(find(sql, param));
    }





    public JSONArray findAllEPCPage(JSONObject param, Integer pageNum, Integer pageSize) {
        String sql = "(SELECT ROWNUM AS rowno, A.*  FROM EPC_BBMC A  WHERE  ROWNUM <=" + pageSize;
        if (param.getString("autoBrand") != null && !"".equals(param.getString("autoBrand")))
            sql += " AND A.QCPPMC = '" + param.getString("autoBrand") + "'";
        if (param.getString("autoLabel") != null && !"".equals(param.getString("autoLabel")))
            sql += " AND A.QCCPMC = '" + param.getString("autoLabel") + "'";
        if (param.getString("epcVersion") != null && !"".equals(param.getString("epcVersion")))
            sql += " AND A.EPC_BBMC = '" + param.getString("epcVersion") + "'";
        String sSQL = "SELECT * FROM " + sql + " ) table_alias   \n" +
                "  WHERE table_alias.rowno >" + pageNum;
        return toJSONArray(find(sSQL, param));
    }

    public JSONArray countEPC(JSONObject param) {

        String sql = "SELECT count(*) AS count FROM EPC_BBMC A WHERE 1=1  ";
        if (param.getString("autoBrand") != null && !"".equals(param.getString("autoBrand")))
            sql += " AND A.QCPPMC = '" + param.getString("autoBrand") + "'";
        if (param.getString("autoLabel") != null && !"".equals(param.getString("autoLabel")))
            sql += " AND A.QCCPMC = '" + param.getString("autoLabel") + "'";
        if (param.getString("epcVersion") != null && !"".equals(param.getString("epcVersion")))
            sql += " AND A.EPC_BBMC = '" + param.getString("epcVersion") + "'";

        return toJSONArray(find(sql, param));
    }




    public List<String> comparison(String oe, List epc){
        String sql = "SELECT PJMCID FROM EPC_OE_PJMC WHERE PARTNUMBER = :oe AND EPC_BBMC IN :epc";
        return find(sql, MapUtil.mapper("oe", oe, "epc", epc));
    }


    public JSONArray findAllOrigin(JSONObject param, Integer pageNum, Integer pageSize) {
        String sql = "(SELECT ROWNUM AS rowno, A.*  FROM EPC_OE_PJMC A  WHERE  ROWNUM <=" + pageSize;
        if (param.getString("name") != null && !"".equals(param.getString("name")))
            sql += " AND A.PJMC = '" + param.getString("name") + "'";
        if (param.getString("oeMark") != null && !"".equals(param.getString("oeMark")))
            sql += " AND A.PARTNUMBER = '" + param.getString("oeMark") + "'";
        if (param.getString("epcVersion") != null && !"".equals(param.getString("epcVersion")))
            sql += " AND A.EPC_BBMC = '" + param.getString("epcVersion") + "'";
        String sSQL = "SELECT * FROM " + sql + " ) table_alias   \n" +
                "  WHERE table_alias.rowno >" + pageNum;
        return toJSONArray(find(sSQL, param));
    }

    public JSONArray countOrigin(JSONObject param) {

        String sql = "SELECT count(*) AS count FROM EPC_OE_PJMC A WHERE 1=1  ";
        if (param.getString("name") != null && !"".equals(param.getString("name")))
            sql += " AND A.PJMC = '" + param.getString("name") + "'";
        if (param.getString("oeMark") != null && !"".equals(param.getString("oeMark")))
            sql += " AND A.PARTNUMBER = '" + param.getString("oeMark") + "'";
        if (param.getString("epcVersion") != null && !"".equals(param.getString("epcVersion")))
            sql += " AND A.EPC_BBMC = '" + param.getString("epcVersion") + "'";

        return toJSONArray(find(sql, param));
    }
}
